<template>
  <!--结案-->
  <div class="page" style="padding-bottom:20px">
    <data-grid iconClass="icon" :labelWidth="100" labelAlign="left" contentAlign="left">
      <data-grid-item label="客户姓名：" :span="6">{{custInfo.custName}}</data-grid-item>
      <data-grid-item label="身份证号：" :span="6">{{caseInfo.cupoIdcard}}</data-grid-item>
      <data-grid-item label="合同金额：" :span="6">{{caseInfo.cupoContnum}}</data-grid-item>
      <data-grid-item label="逾期天数：" :span="6">{{caseInfo.cupoOverday}}</data-grid-item>
      <data-grid-item label="案件金额：" :span="6">{{caseInfo.cupoAmt}}</data-grid-item>
      <data-grid-item label="实际还款金额：" :span="6">{{caseInfo.cupoPaysum}}</data-grid-item>
      <data-grid-item label="结案方式:" :span="12">
        <el-select v-model="forms.cupoEndtype" placeholder="请选择" clearable @change="endcaseMethodsClick">
          <el-option v-for="{id,name} in this.$helper.getTypeCode('0043')" :key="id" :label="name" :value="id">
          </el-option>
        </el-select>
      </data-grid-item>
      <data-grid-item label="结案说明:" :span="12">
        <el-row>
          <el-col :span="24">
            <el-input v-model="forms.cupoRemark" type="textarea" placeholder="请输入结案说明" :maxlength="500"></el-input>
          </el-col>
        </el-row>
      </data-grid-item>
    </data-grid>
    <el-button @click="actionCommit">确定</el-button>
    <el-button @click="cancelButton">取消</el-button>
  </div>
</template>
<script>
  import servers from '@/config/servers'
  import net from '@/util/net'
  import DataGrid from '@/components/DataGrid'
  import DataGridItem from '@/components/DataGridItem'
  import { mapState, mapMutations } from 'vuex'
  import FileUpload from '@/components/FileUpload'

  export default {
    name: 'end-case',
    data() {
      return {
        forms: {
          cupoRemark: '',
          cupoEndtype: []
        }
      }
    },
    computed: {
      ...mapState({
        custInfo: state => state.accManage.cust_info,
        caseInfo: state => state.accManage.case_info,
        type: state => state.accManage.type
      })
    },
    methods: {
      ...mapMutations([
        'saveUpdateTime'
      ]),
      back() {
        this.saveUpdateTime()
        this.$router.push({ name: this.type })
      },
      /**
       * 结案方式change事件
       */
      endcaseMethodsClick() {
        let endCaseType = this.forms.cupoEndtype
        if (endCaseType === 188) {
          this.forms.cupoRemark = '司法结案'
        }
        if (endCaseType === 189) {
          this.forms.cupoRemark = '债主死亡'
        }
        if (endCaseType === 190) {
          this.forms.cupoRemark = '已还款'
        }
        if (endCaseType === 191) {
          this.forms.cupoRemark = '批量结案'
        }
      },
      /**
       * 结案确定
       */
      actionCommit() {
        if (this.forms.cupoRemark === '') {
          this.$alert('请输入结案说明', '提示', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.caseInfo.cupoPaysum < this.custInfo.cupoAmt) {
          this.$confirm('该案件实际还款金额小于案件金额，确定要结案吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // this.forms.caseId = this.$store.state.accManage.case_id
            this.forms.caseNum = this.$store.state.accManage.case_num
            net.send({
              server: servers.CaseManage.get_end_case_info,
              data: this.forms
            }).then((data) => {
              this.$alert('该案件成功结案，如需查看，请前往已处理记录！', '提示', {
                confirmButtonText: '知道了',
                type: 'sucess'
              })
              this.back()
              this.$emit('success')
              this.$emit('close')
              // 重置
              this.forms = ''
            }, err => {
              this.$message({
                type: 'info',
                message: err.reason
              })
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消结案'
            })
          })
        } else {
          this.forms.caseNum = this.$store.state.accManage.case_num
          net.send({
            server: servers.CaseManage.get_end_case_info,
            data: this.forms
          }).then((data) => {
            this.$alert('该案件成功结案，如需查看，请前往已处理记录！', '提示', {
              confirmButtonText: '知道了',
              type: 'sucess'
            })
            this.$emit('close')
            this.$emit('success')
            this.back()
            // 重置
            this.forms.cupoRemark = ''
            this.forms.cupoEndtype = []
          }, err => {
            this.$message({
              type: 'info',
              message: err.reason
            })
          })
        }
      },
      /**
       * 取消结案
       */
      cancelButton() {
        this.$emit('close')
        // 重置
        this.forms.cupoRemark = ''
        this.forms.cupoEndtype = []
      }
    },
    components: {
      DataGrid,
      DataGridItem,
      FileUpload
    },
    mounted() {}
  }

</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
  @import '../../styles/variable.less';
  .data-grid {
    margin-bottom: 10px;
  }
  
  .header {
    border: solid 1px @-border-color;
    border-bottom: 0px;
    height: 40px;
    line-height: 40px
  }

</style>
